<template>
    <div>
        <Row>
            <i-col span="24">
                <Card>
                    <Button class="opt-btn" @click="showAddModal" type="primary">添加</Button>
                    <Dropdown class="opt-btn" @on-click="batchOpt">
                        <Button type="primary">
                            批量操作
                            <Icon type="ios-arrow-down"></Icon>
                        </Button>
                        <DropdownMenu slot="list">
                            <DropdownItem name="batchActive">批量激活</DropdownItem>
                            <DropdownItem name="batchInactive"><span style="color: red;">批量冻结</span></DropdownItem>
                            <DropdownItem name="batchRemove" divided><span style="color: red;">批量删除</span></DropdownItem>
                        </DropdownMenu>
                    </Dropdown>
                    <Button class="opt-btn" @click="showSearchModal" type="primary">高级搜索</Button>
                    <Tooltip class="opt-btn" content="刷新" placement="right">
                        <Button icon="md-refresh" type="success" shape="circle" @click="searchTable(false)"></Button>
                    </Tooltip>
                    <Tooltip class="opt-btn" content="重置搜索" placement="right">
                        <Button icon="md-repeat" type="success" shape="circle" @click="researchTable"></Button>
                    </Tooltip>
                    <ArticleTableMain ref="table" v-on:searchTable="searchTable" v-on:showEditModal="showEditModal" 
                    v-on:showDetailModal="showDetailModal" v-on:showContentModal="showContentModal"
                    v-on:showModuleDetail="userModuleDetail" v-on:showModuleSearch="userModuleSearch"/>
                </Card>
            </i-col>
        </Row>
        <ArticleAddEditModal ref="addEditModal" v-on:add="add" v-on:edit="edit"/>
        <ArticleSearchModal ref="searchModal" v-on:searchTable="searchTable"/>
        <ArticleDetailModal ref="detailModal"/>
        <ContentModal ref="contentModal"/>
        <UserUserDetailDetailModal ref="moduleDetail" />
        <!-- 搜索用户 -->
         <Modal
          v-model="modal.moduleSearch"
          title="用户查询"
          :mask-closable="false"
          width="1020"
        >
          <UserUserDetailMainSingle 
            ref="moduleSearch"
            v-on:confirmChoice="confirmChoice"
          />
          <div slot="footer">
            <Button type="text" size="large" @click="cancelModal('moduleSearch')">取消</Button>
            <Button type="primary" size="large" @click="bottomConfirmChoice">确认选择</Button>
          </div>
        </Modal>
    </div>
</template>

<script>
    import * as utils from '@/api/utils-v2'
    import * as ResponseStatus from '@/api/response-status'
    import UserUserDetailMainSingle from '../user-userdetail/UserUserDetailMainSingle.vue'
    import UserUserDetailDetailModal from '../user-userdetail/UserUserDetailDetailModal.vue'
    import ArticleTableMain from './ArticleTableMain.vue'
    import ArticleAddEditModal from './ArticleAddEditModal.vue'
    import ArticleSearchModal from './ArticleSearchModal.vue'
    import ArticleDetailModal from './ArticleDetailModal.vue'
    import ContentModal from '_c/content-modal'

    import config from '@/config'
    const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro
    const cdnUrl = config.baseUrl.cdnUrl
    export default {
        name: 'ArticleMain',
        components: {
            UserUserDetailDetailModal,
            UserUserDetailMainSingle,
            ArticleTableMain,
            ArticleAddEditModal,
            ArticleSearchModal,
            ArticleDetailModal,
            ContentModal
        },
        data() {
            return {
                modal: {
                    moduleSearch:false
                },
                urls: {
                    batchRemoveUrl: '/article/admin/batch-remove',
                    batchActiveUrl: '/article/admin/batch-active',
                    oneUrl: '/article-category/admin/one/'
                },
            }
        },
        computed: {},
        mounted() {},
        methods: {
            searchTable(reset) {
                utils.search(this, reset)
            },
            researchTable() {
                this.$refs.searchModal.resetForm()
                setTimeout(() => {
                    utils.search(this, true)
                }, 10);
            },
            /**
             * 显示用户模块详情
             */
            userModuleDetail(id) {
              //this.$refs.moduleDetail.form = {}
              utils
                .doGet('/user-userdetail/admin/multi/' + id, {})
                .then(res => {
                  if (ResponseStatus.OK === res.data.code) {
                    let moduleDetail = this.$refs.moduleDetail
                    moduleDetail.form = res.data.data.rows[0]
                    moduleDetail.modal.detail = true
                    moduleDetail.getDictItemsDisplayValue()
                  } else {
                    this.$Message.error(res.data.message)
                  }
                })
                .catch(err => {
                  console.log(err)
                })
            },
            /**
             * 显示用户模块搜索
             */
            userModuleSearch() {
              this.showModal('moduleSearch')
              this.$refs.moduleSearch.searchTable(true)
            },
            /**
             * 底部的确认选择
             */
            bottomConfirmChoice() {
              this.$refs.moduleSearch.confirmSelection()
            },
             /**
             * 确认选择
             */
            confirmChoice(row) {
                this.cancelModal('moduleSearch')
                let searchModal = this.$refs.searchModal
                searchModal.searchForm.createIdMin =  row.userId
                searchModal.searchForm.createIdMax =  row.userId                   
               this.searchTable()
            },
            showModal(modal) {
              utils.showModal(this, modal)
            },
            cancelModal(modal) {
              utils.cancelModal(this, modal)
            },
            showAddModal() {
                let addEditModal = this.$refs.addEditModal
                addEditModal.$refs.editorAdd.initEditor('/article/admin/upload-img')
                addEditModal.$refs.editorAdd.setHtml('')
                addEditModal.modal.add = true
            },
            add() {
                utils.add(this)
            },
            showEditModal(row) {
                let addEditModal = this.$refs.addEditModal
                utils
                  .doGet(this.urls.oneUrl + row.categoryId, {})
                  .then(res => {
                    if (ResponseStatus.OK === res.data.code) {
                      const title = res.data.data.title
                      if (title) {
                        row.categoryName = title
                      }
                      if (row.coverImg) {
                        row.url = cdnUrl + "/" + row.coverImg
                      }
                      addEditModal.modal.edit = true
                      row.version = null
                row.createTime = null
                row.updateTime = null
                addEditModal.form = row
                      addEditModal.$refs.editorEdit.initEditor('/article/admin/upload-img')
                      addEditModal.$refs.editorEdit.setHtml(row.content)
                      
                    } else {
                      this.$Message.error(res.data.message)
                    }
                  })
                  .catch(err => {
                    console.log(err)
                  })
            },
            edit() {
                utils.edit(this)
            },
            showDetailModal(row) {
                let detailModal = this.$refs.detailModal
                detailModal.modal.detail = true
                detailModal.form = row
				detailModal.getDictItemsDisplayValue()
            },
            showSearchModal() {
                let searchModal = this.$refs.searchModal
                searchModal.modal.search = true
            },
            batchOpt(itemName) {
                if (itemName === 'batchActive') {
                    utils.batchActive(this, 0)
                } else if (itemName === 'batchInactive') {
                    utils.batchActive(this, 1)
                } else if (itemName === 'batchRemove') {
                    utils.batchRemove(this)
                }
            },
            showContentModal(content) {
                let contentModal = this.$refs.contentModal
                contentModal.title = '文章内容'
                contentModal.content = content
                contentModal.contentModal = true
            }
        }
    }
</script>

<style>
</style>
